package com.iraeta.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.entity.CustomRole;
import com.iraeta.entity.User;
import com.iraeta.service.sys.RoleService;
import com.iraeta.service.sys.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * 外部客户的维护 会有登录信息
 * */
@RestController(value = "WLUserController")
@RequestMapping("emp/wluser")
public class WLUserController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(WLUserController.class);
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping("pagewluser")
    public String pageWLUser(@RequestBody JSONObject jo){
        String userName = jo.getString("userName");
        this.pageSet(jo);
        List<User> userList = userService.listWuLiuUserByName(userName);
        PageInfo<User> userPageInfo = this.getPageInfo(userList);
        return JSON.toJSONString(Result.success(userPageInfo));
    }

    @RequestMapping("getwluser")
    public String getWLUser(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("id");
        User user = userService.getUser(userId);
        return JSON.toJSONString(Result.success(user));
    }

    @RequestMapping("addeditwluser")
    public String addWLUser(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("id");
        User user = userId == 0 ? new User() :  userService.getUser(userId);
        this.mapJsonToObj(paramJb, user);
        if(userId == 0){
            user.setPhoto("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            userService.addUser(user, CustomRole.wluser);
        }else {
            userService.updateUser(user);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("listallwluser")
    public String listAllWLUser(){
        List<User> keHuList = userService.listWuLiuUserByName(null);
        return this.success(keHuList);
    }
}
